<?php
/**
 * AuthMenu
 *
 * @Description 描述
 * @Author look
 * @CreateTime 2022/2/23
 */

namespace app\common\utils;

use app\admin\service\AdminService;
use app\common\exception\ApiException;

/**
 * 资源访问权限
 */
class AuthMenu {

    public function __construct(
        private int $admin_id,
        private string $perm
    ) {}

    public function checkAuth() {
        // 可以访问的地址
        $menus = (new AdminService())->getMenusByAdmin($this->admin_id);
        $auth = false;

        foreach($menus as $menu) {
            if ($menu->perm === $this->perm) {
                $auth = true;
            }
        }
        if (!$auth) {
            throw new ApiException(...ErrorCode::NO_AUTH);
        }
    }

}